package org.jeecg.modules.app.v1.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ApiLogin;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.aspct.ApiCheckDataAuth;
import org.jeecg.modules.app.v1.device.entity.*;
import org.jeecg.modules.app.v1.home.entity.ApiHomeDeviceEntity;
import org.jeecg.modules.aspect.ApiLoginAspct;
import org.jeecg.modules.power.device.entity.YlDevice;
import org.jeecg.modules.power.device.entity.YlDeviceOperateRecords;
import org.jeecg.modules.power.device.entity.YlDeviceRecords;
import org.jeecg.modules.power.device.service.IYlDeviceOperateRecordsService;
import org.jeecg.modules.power.device.service.IYlDeviceRecordsService;
import org.jeecg.modules.power.device.service.IYlDeviceService;
import org.jeecg.modules.power.records.service.IYlDeviceStatusRecordsService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 设备表
* @Author: jeecg-boot
* @Date:   2024-07-16
* @Version: V1.0
*/
   @Api(tags="APP管理端_设备")
@RestController
@RequestMapping("/api/v1/device")
@Slf4j
@RequiredArgsConstructor
public class ApiDeviceController {

   private final IYlDeviceService ylDeviceService;
   private final IYlDeviceRecordsService deviceRecordsService;
   private final IYlDeviceStatusRecordsService deviceStatusRecordsService;
   private final IYlDeviceOperateRecordsService deviceOperateRecordsService;

   /**
    * 资产一览接口待确认
    */
   @ApiOperation(value="1_资产一览", notes="资产一览")
   @PostMapping(value = "/getAll")
   @ApiOperationSupport(order = 1)
   @ApiCheckDataAuth
   public Result<List<ApiDeviceCategoryEntity>> getAll(String flag,String groupId,HttpServletRequest request){
      List<ApiDeviceCategoryEntity> list = ylDeviceService.getAllDeviceByCategory(flag, groupId);
      return Result.OK(list);
   }

   /**
    * 资产一览接口待确认
    */
   @ApiOperation(value="2_资产一览_下级数据", notes="资产一览_下级数据")
   @PostMapping(value = "/getBelowAll")
   @ApiOperationSupport(order = 2)
   @ApiCheckDataAuth
   public Result<List<ApiDeviceCategoryEntity>> getBelowAll(String flag,String groupId,HttpServletRequest request){
      List<ApiDeviceCategoryEntity> list = ylDeviceService.getBelowDeviceByCategory(flag, groupId);
      return Result.OK(list);
   }
   /**
    * 资产一览接口待确认
    */
   @ApiOperation(value="3_资产一览_故障设备_下级数据", notes="资产一览_故障设备_下级数据")
   @PostMapping(value = "/getFaultDevice")
   @ApiOperationSupport(order = 3)
   @ApiCheckDataAuth
   public Result<List<ApiDeviceCategoryEntity>> getFaultDevice(String flag,String groupId,HttpServletRequest request){
      List<ApiDeviceCategoryEntity> list = ylDeviceService.getFaultDeviceByCategory(flag, groupId);
      return Result.OK(list);
   }

   /**
    * 资产一览接口待确认
    */
   @ApiOperation(value="4_资产一览_故障设备_BD/门店下钻接口", notes="资产一览_故障设备_下级数据")
   @PostMapping(value = "/faultDeviceList")
   @ApiImplicitParams({
           @ApiImplicitParam(name="categoryId",value = "设备分类ID"),
           @ApiImplicitParam(name="flag",value = "1小组2BD"),
           @ApiImplicitParam(name="groupId",value = "flag=1时小组Id，flag=2时bdId"),
           @ApiImplicitParam(name="mac",value = "设备mac，模糊搜索"),
           @ApiImplicitParam(name="sourceType",value = "2BD3门店"),
           @ApiImplicitParam(name="status",value = "1待审核2已完成"),
   })
   @ApiOperationSupport(order = 4)
   @ApiCheckDataAuth
   public Result<List<ApiFaultDeviceEntity>> faultDeviceList(String categoryId,String flag,String groupId, String mac,String sourceType,String status,
                                                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest request){
      Page<ApiFaultDeviceEntity> page = new Page<>(pageNo,pageSize);
      IPage<ApiFaultDeviceEntity> list = deviceRecordsService.faultDeviceList(page,mac,categoryId,flag, groupId,sourceType,status);
      return Result.OK(list.getRecords());
   }


   /**
    *  资产BD
    * @param request
    * @return
    */
   @ApiOperation(value="4_资产BD", notes="资产BD")
   @PostMapping(value = "/bdProperty")
   @ApiOperationSupport(order = 4)
   @ApiCheckDataAuth
   public Result<?> bdProperty( String categoryId,String flag,String groupId, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest request) {
      SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
      Page<ApiDeviceEntity> page = new Page<>(pageNo,pageSize);
      List<ApiDeviceEntity> list = ylDeviceService.bdDeviceList(page,categoryId,flag,groupId);
      return Result.OK(list);
   }
   /**
    * 资产门店
    * @param request
    * @return
    */
   @ApiOperation(value="5_资产门店", notes="资产门店")
   @PostMapping(value = "/shopProperty")
   @ApiOperationSupport(order = 5)
   @ApiCheckDataAuth
   public Result<?> shopProperty(String categoryId,String flag,String groupId,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest request) {
      SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
      Page<ApiDeviceEntity> page = new Page<>(pageNo,pageSize);
      List<ApiDeviceEntity> list = ylDeviceService.shopDeviceList(page,categoryId,flag,groupId);
      return Result.OK(list);
   }

   /**
    * 资产仓库
    * @param request
    * @return
    */
   @ApiOperation(value="6_资产仓库", notes="资产仓库")
   @PostMapping(value = "/storeProperty")
   @ApiOperationSupport(order = 6)
   @ApiCheckDataAuth
   public Result<?> storeProperty(String categoryId,String flag,String groupId,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest request) {
      Page<ApiDeviceEntity> page = new Page<>(pageNo,pageSize);
      List<ApiDeviceEntity> list = ylDeviceService.storeDeviceList(page,categoryId);
      return Result.OK(list);
   }

   @ApiLogin
   @ApiOperation(value="7_扫码校验新增门店绑定设备", notes="扫码校验新增门店绑定设备")
   @GetMapping(value = "/check")
   @ApiOperationSupport(order = 7)
   @ApiImplicitParams({
           @ApiImplicitParam(value = "设备编号",name = "deviceMac"),
   })
   public Result<?> check(String deviceMac,HttpServletRequest request) {
      SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
      JSONObject result = deviceRecordsService.getNewDevice(deviceMac,bd.getId());
      if(oConvertUtils.isEmpty(result))return Result.error_599("未查询到该设备信息");
      return Result.OK(result);
   }

   @ApiLogin
   @ApiOperation(value="8_门店设备列表（需添加具体设备在线情况）", notes="门店设备列表")
   @GetMapping(value = "/shopDeviceList")
   @ApiOperationSupport(order = 8)
   public Result<?> shopDeviceList(String shopId) {
      List<ApiShopDeviceEntity> result = ylDeviceService.queryShopDevice(shopId);
      return Result.OK(result);
   }

   @ApiLogin
   @ApiOperation(value="9_门店设备详情", notes="门店设备详情")
   @GetMapping(value = "/shopDeviceDetail")
   @ApiOperationSupport(order = 9)
   public Result<?> shopDeviceDetail(String deviceId) {
      ApiShopDeviceDetailEntity result = ylDeviceService.queryShopDeviceDetail(deviceId);
      return Result.OK(result);
   }


   @ApiLogin
   @ApiOperation(value="10_门店统计模块-设备统计数据", notes="门店统计模块-设备统计数据")
   @GetMapping(value = "/shopDevice")
   @ApiOperationSupport(order = 10)
   public Result<ApiHomeDeviceEntity> countShopDevice(String shopId) {
      ApiHomeDeviceEntity device = deviceStatusRecordsService.countShopDevice(shopId);
      return Result.OK(device);
   }
   @ApiLogin
   @ApiOperation(value="11_门店统计模块-设备详情-设备离线记录", notes="门店统计模块-设备详情-设备离线记录")
   @GetMapping(value = "/offlineRecords")
   @ApiOperationSupport(order = 11)
   public Result<List<ApiDeviceOfflineEntity>> deviceOfflineRecords(String deviceId,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
      Page<ApiDeviceOfflineEntity> page = new Page<>(pageNo,pageSize);
      IPage<ApiDeviceOfflineEntity> pageRecords = deviceStatusRecordsService.offlineRecords(page,deviceId);
      return Result.OK(pageRecords.getRecords());
   }

   /**
    * 资产仓库
    * @param type 运维类型 type:1强制弹宝2正常弹宝3锁仓4解锁
    * @param hatch 仓口号，多个，隔开
    * @param reason 锁仓或者解仓原因
    * @param request
    * @return
    */
   @ApiOperation(value="12_仓宝运维(type:1强制弹宝2正常弹宝3锁仓4解锁)", notes="仓宝运维(1强制弹宝2正常弹宝3锁仓4解锁)")
   @PostMapping(value = "/operate")
   @ApiOperationSupport(order = 12)
   @ApiLogin
   public Result<?> operate(String deviceId,String deviceMac,Integer type,String hatch,String reason,HttpServletRequest request) {
      SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
      ylDeviceService.operate(deviceId,deviceMac,type,hatch,reason,bd);
      return Result.OK();
   }

   @ApiLogin
   @ApiOperation(value="13_仓宝运维记录", notes="仓宝运维记录")
   @GetMapping(value = "/operationRecords")
   @ApiOperationSupport(order = 13)
   public Result<List<YlDeviceOperateRecords>> deviceOperationRecords(String deviceId,String beginTime,String endTime,String type,
                                                                      @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
      Page<YlDeviceOperateRecords> page = new Page<>(pageNo,pageSize);
      List<YlDeviceOperateRecords> pageRecords = deviceOperateRecordsService.getRecords(page,deviceId,beginTime,endTime,type);
      return Result.OK(pageRecords);
   }


}
